package br.uff.probabilidade.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.awt.peer.KeyboardFocusManagerPeer;

public abstract class Atividade extends Applet implements MouseListener,ImageObserver{	
	
	private static final Color BACKGROUND_COLOR = new Color(200,200,200);
	
/**
 * <p>Posicao no applet</p>
 */
	protected Point posicao;
	protected MediaTracker mediaTracker;	
	
	private boolean dbon = true;	
	private Image offscreen;
	
	public Point getPosicao(){
		return posicao;
	}
	
	protected void setDoubleBuffered(boolean dbon){
		this.dbon = dbon;
	}
	
	protected void atualiza() {
		if (dbon) {
			updateDoubleBuffered(this.getGraphics());
		} else {
			super.update(this.getGraphics());
		}
	}
	
	protected void updateDoubleBuffered(Graphics g) {
		int width = -1;
		int height = -1;
		Dimension d = getSize();
		if (offscreen == null || width != d.width || height != d.height
				|| offscreen == null) {
			width = d.width;
			height = d.height;
			if (width > 0 || height > 0) {
				offscreen = createImage(width, height);
			} else
				offscreen = null;
		}

		if (offscreen == null)
			return;

		Graphics gg = offscreen.getGraphics();

		gg.setColor(getBackground());
		gg.fillRect(0, 0, width, height);
		gg.setColor(getForeground());

		paint(gg);

		gg.dispose();

		g.drawImage(offscreen, 0, 0, null);
	}
	protected void setDefaultAttributes(){
		this.setSize(600, 400);
		this.setBackground(BACKGROUND_COLOR);
	}
	abstract public void desenha(Graphics g);
	abstract public void init();
	abstract public void paint(Graphics g);
}
